// 抽象工厂模式
#include <iostream>

using namespace std;

class Shape {  // 基础图形接口
public:
    virtual void draw() = 0;
    virtual double calculateArea() = 0;
    virtual ~Shape() {};
};

class Rectangle : public Shape {  // 矩形
public:
    void draw() override { cout << "Drawing a rectangle\n"; }
    double calculateArea() override {
        double width = 5;
        double height = 3;
        return width * height;
    }
};

class Circle : public Shape {  // 圆形
public:
    void draw() override { cout << "Drawing a circle\n"; }
    double calculateArea() override {
        double radius = 2;
        return 3.14159 * radius * radius;
    }
};

class AbstractFactory {  // 抽象工厂类
public:
    virtual Shape* createShape() = 0;
    virtual ~AbstractFactory() {};
};

class RectangleFactory : public AbstractFactory {  // 具体矩形工厂
public:
    Shape* createShape() override { return new Rectangle(); }
};

class CircleFactory : public AbstractFactory {  // 具体圆形工厂
public:
    Shape* createShape() override { return new Circle(); }
};

int main() {
    AbstractFactory* factory = nullptr;
    Shape* shape = nullptr;

    factory = new RectangleFactory();
    shape = factory->createShape();
    if (shape) {
        shape->draw();
        double area = shape->calculateArea();
        cout << "Area: " << area << '\n';
    } else {
        cout << "Unsupported shape\n";
    }

    factory = new CircleFactory();
    shape = factory->createShape();
    if (shape) {
        shape->draw();
        double area = shape->calculateArea();
        cout << "Area: " << area << '\n';
    } else {
        cout << "Unsupported shape\n";
    }
    return 0;
}